/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import pojo.Chitiethoadondeal;
import util.HibernateUtil;

/**
 *
 * @author Katori
 */
public class ChiTietHoaDonDealDAO {

    public static List<Chitiethoadondeal> layTatCaChiTietHoaDonDeal() {
        List<Chitiethoadondeal> dsChiTiet = null;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.getCurrentSession();
        Transaction trans = ss.getTransaction();
        trans.begin();
        try {
            String hql = "from pojo.Chitiethoadondeal";
            dsChiTiet = ss.createQuery(hql).list();
            trans.commit();
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        }
        return dsChiTiet;
    }

    public static Chitiethoadondeal layChiTietHoaDonDeal(int maChiTiet) {
        Chitiethoadondeal chiTiet = null;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.getCurrentSession();
        Transaction trans = ss.getTransaction();
        trans.begin();
        try {
            chiTiet = (Chitiethoadondeal) ss.get(Chitiethoadondeal.class, maChiTiet);
            trans.commit();
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
        }
        return chiTiet;
    }

    public static boolean themChiTietHoaDonDeal(Chitiethoadondeal chiTiet) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(chiTiet);
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(e);
            return false;
        } finally {
            session.close();
        }
    }

    public static boolean capNhatChiTietHoaDonDeal(Chitiethoadondeal chiTiet) {
        if (ChiTietHoaDonDealDAO.layChiTietHoaDonDeal(chiTiet.getMaChiTietHoaDonDeal()) == null) {
            return false;
        }

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.getTransaction().begin();
            session.update(chiTiet);
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.print(e);
            return false;
        }
    }

    public static boolean xoaChitiethoadondeal(int maChitiethoadondeal) {
        if (ChiTietHoaDonDealDAO.layChiTietHoaDonDeal(maChitiethoadondeal) == null) {
            return false;
        }

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            session.getTransaction().begin();
            String hql = "update pojo.Chitiethoadondeal set BiXoa = :xoa where MaChitiethoadondeal = :ma";
            Query query = session.createQuery(hql);
            query.setString("xoa", "1");
            query.setString("ma", Integer.toString(maChitiethoadondeal));
            int rowCount = query.executeUpdate();
            session.getTransaction().commit();

            if (rowCount > 0) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.print(e);
            return false;
        }
    }
}
